Daniela’s Notes for Nicemboim, Schad, & Vasishth (2024)
  • D. Palleschi
  1. Book notes
  2. 3  Contrast Coding
  • 1  workbook
  • Book notes
    • 2  Chapter notes
    • 3  Contrast Coding
  • Book exercises
    • 4  Ch. 1 Exercises
    • 5  Ch. 8 Exercises
  • Literaturverzeichnis

Chatper contents

  • Set up
    • 3.1 Data
    • 3.2 Basic concepts illustrated using a two-level factor
      • 3.2.1 Default contrast coding
      • 3.2.2 Defining comparions
      • 3.2.3 Sum contrasts
      • 3.2.4 Cell means parameterization
    • 3.3 The hypohtesis matrix
      • 3.3.1 Sum contrasts
      • 3.3.2 They hypothesis matrix
      • 3.3.3 Generating contrasts: the hypr package
    • 3.4 Other types of contrasts:
      • 3.4.1 Repeated contrasts
      • 3.4.2 Helmert contrasts
      • 3.4.3 Contrasts in linear regression
      • 3.4.4 Plynomial contrasts
      • 3.4.5 An alternative to contrasts
    • 3.5 What makes a good set of contrasts
      • 3.5.1 Centered contrasts
      • 3.5.2 Orthogonal contrasts
      • 3.5.3 The role of the intercept
    • 3.6 Computing condition means
    • 3.7 Summary
  1. Book notes
  2. 3  Contrast Coding

3  Contrast Coding

These notes accompany Ch. 8 (Contrast coding) of the current version of Bayesian Data Analysis for Cognitive Science []

We will work with a three-level factor, e.g., three levels of word class: noun, verb, adjective. Does word class influence response times? In Frequentist: we could run an ANOVA and compute omnibus F-test. In Bayesian, the equivalent is a Bayesian model comparison (i.e., Bayes factors): we can compare alternative model with word class as a predictor to a null model (this topic comes up in Ch. 15). Both approaches don’t tell us anything about the actual influence of the respective levels of word class and how they differ, all we now is that they differ: do nouns have faster RTs than verbs, but not adjectives (or any other suc combination of relationships between the three)?

Enter: contrast coding. We can plan comparisons between conditions/groups of conditions a priori in order to define our predictions/research questions explicitly in a model.

Set up

# set global knit options
knitr::opts_chunk$set(echo = T, # print chunks?
                      eval = T, # run chunks?
                      error = F, # print errors?
                      warning = F, # print warnings?
                      message = F, # print messages?
                      cache = T # cache?; be careful with this!
                      )

# suppress scientific notation
options(scipen=999)

# play a sound if error encountered
options(error = function() {beepr::beep(9)})

# load packages
## create list of package names
packages <- c( #"SIN", # this package was removed from the CRAN repository
               "MASS", "dplyr", "tidyr", "purrr", "extraDistr", "ggplot2", "loo", "bridgesampling", "brms", "bayesplot", "tictoc", "hypr", "bcogsci", "papaja", "grid", "kableExtra", "gridExtra", "lme4", "cowplot", "pdftools", "cmdstanr", "rootSolve", "rstan"
  )

# NB: if you haven't already installed bcogsci through devtools, it won't be loaded
## Now load or install & load all
package.check <- lapply(
  packages,
  FUN = function(x) {
    if (!require(x, character.only = TRUE)) {
      install.packages(x, dependencies = TRUE)
      library(x, character.only = TRUE)
    }
  }
)

# this is also required, taken from the textbook

## Save compiled models:
rstan_options(auto_write = FALSE)
## Parallelize the chains using all the cores:
options(mc.cores = parallel::detectCores())
# To solve some conflicts between packages
select <- dplyr::select
extract <- rstan::extract

3.1 Data

Load in a simulated dataset with RTs for two conditions: F1 and F2.

data("df_contrasts1")
head(df_contrasts1)
# A tibble: 6 × 3
  F        DV    id
  <fct> <dbl> <int>
1 F1    0.636     1
2 F1    0.841     2
3 F1    0.555     3
4 F1    1.03      4
5 F1    0.938     5
6 F2    0.123     6

3.2 Basic concepts illustrated using a two-level factor

3.2.1 Default contrast coding

3.2.2 Defining comparions

3.2.3 Sum contrasts

3.2.4 Cell means parameterization

3.3 The hypohtesis matrix

3.3.1 Sum contrasts

3.3.2 They hypothesis matrix

3.3.3 Generating contrasts: the hypr package

  • to use the 4-step procedure (i.e., to flexibly design contrasts to estimate specific comparisons), the authors developed the hypr package
    • can specify desired comparisons and generate contrast matrices

3.4 Other types of contrasts:

3.4.1 Repeated contrasts

3.4.2 Helmert contrasts

3.4.3 Contrasts in linear regression

3.4.4 Plynomial contrasts

3.4.5 An alternative to contrasts

3.5 What makes a good set of contrasts

3.5.1 Centered contrasts

3.5.2 Orthogonal contrasts

3.5.3 The role of the intercept

3.6 Computing condition means

3.7 Summary

2  Chapter notes
4  Ch. 1 Exercises
Quellcode
---
title: "Contrast Coding"
---

These notes accompany [Ch. 8 (Contrast coding)](https://vasishth.github.io/bayescogsci/book/ch-contr.html) of the current version of [Bayesian Data Analysis for Cognitive Science](https://vasishth.github.io/bayescogsci) []

We will work with a three-level factor, e.g., three levels of word class: noun, verb, adjective. Does word class influence response times? In Frequentist: we could run an ANOVA and compute omnibus F-test. In Bayesian, the equivalent is a Bayesian model comparison (i.e., Bayes factors): we can compare alternative model with word class as a predictor to a null model (this topic comes up in Ch. 15). Both approaches don't tell us anything about the actual influence of the respective levels of word class and how they differ, all we now is *that* they differ: do nouns have faster RTs than verbs, but not adjectives (or any other suc combination of relationships between the three)?

Enter: contrast coding. We can plan comparisons between conditions/groups of conditions a priori in order to define our predictions/research questions explicitly in a model.

# Set up {-}

```{r, results = "hide", warning=F,message=F,error=F}
# set global knit options
knitr::opts_chunk$set(echo = T, # print chunks?
                      eval = T, # run chunks?
                      error = F, # print errors?
                      warning = F, # print warnings?
                      message = F, # print messages?
                      cache = T # cache?; be careful with this!
                      )

# suppress scientific notation
options(scipen=999)

# play a sound if error encountered
options(error = function() {beepr::beep(9)})

# load packages
## create list of package names
packages <- c( #"SIN", # this package was removed from the CRAN repository
               "MASS", "dplyr", "tidyr", "purrr", "extraDistr", "ggplot2", "loo", "bridgesampling", "brms", "bayesplot", "tictoc", "hypr", "bcogsci", "papaja", "grid", "kableExtra", "gridExtra", "lme4", "cowplot", "pdftools", "cmdstanr", "rootSolve", "rstan"
  )

# NB: if you haven't already installed bcogsci through devtools, it won't be loaded
## Now load or install & load all
package.check <- lapply(
  packages,
  FUN = function(x) {
    if (!require(x, character.only = TRUE)) {
      install.packages(x, dependencies = TRUE)
      library(x, character.only = TRUE)
    }
  }
)

# this is also required, taken from the textbook

## Save compiled models:
rstan_options(auto_write = FALSE)
## Parallelize the chains using all the cores:
options(mc.cores = parallel::detectCores())
```


```{r, results = "hide", warning=F,message=F,error=F}
# To solve some conflicts between packages
select <- dplyr::select
extract <- rstan::extract
```

## Data

Load in a simulated dataset with RTs for two conditions: F1 and F2.

```{r}
data("df_contrasts1")
```

```{r}
head(df_contrasts1)
```

## Basic concepts illustrated using a two-level factor

### Default contrast coding

### Defining comparions

### Sum contrasts

### Cell means parameterization

## The hypohtesis matrix

### Sum contrasts

### They hypothesis matrix

### Generating contrasts: the `hypr` package

- to use the 4-step procedure (i.e., to flexibly design contrasts to estimate specific comparisons), the authors developed the `hypr` package
  + can specify desired comparisons and generate contrast matrices
  
  
## Other types of contrasts:

### Repeated contrasts

### Helmert contrasts

### Contrasts in linear regression

### Plynomial contrasts

### An alternative to contrasts

## What makes a good set of contrasts

### Centered contrasts

### Orthogonal contrasts

### The role of the intercept

## Computing condition means

## Summary